package com.kbbywyclient.ui.activity;

import android.webkit.WebSettings;

import com.kbbywyclient.R;
import com.kbbywyclient.app.API;
import com.kbbywyclient.data.entity.Event;
import com.kbbywyclient.di.AppComponent;
import com.kbbywyclient.ui.activity.base.BaseActivity;
import com.kbbywyclient.util.JSInterface;
import com.kbbywyclient.util.LogUtil;
import com.kbbywyclient.util.pre.PreUtils;
import com.kbbywyclient.widget.ProgressWebView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;

/**
 * Description:
 * Created by FHW
 * on 2017/4/24
 */

public class DiscountActivity extends BaseActivity {

    @BindView(R.id.wv_contact)
    ProgressWebView wv_contact;

    private int mType = -1;

    private String url = "";

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_discount;
    }

    @Override
    protected void onResume() {
        super.onResume();
        switch (mType) {
            case 0://匹配
                setScreenName("贷款-匹配申请表-优惠活动");
                break;
            case 1://产品
                setScreenName("贷款-产品申请表-优惠活动");
                break;
            case 2://已购买
                setScreenName("贷款-已购买客户表-买断功能介绍");
                break;
            case 3://客户
                setScreenName("信用卡-客户申请表-优惠活动");
                break;
            case 4://已购买
                setScreenName("信用卡-已购买客户表-买断功能介绍");
                break;
        }
    }

    @Override
    protected void initView() {
        EventBus.getDefault().register(this);

        mType = getIntent().getIntExtra("type", -1);

        //设置webview
        wv_contact.getSettings().setDefaultTextEncodingName("UTF-8");
        //webview自适应屏幕
        WebSettings settings = wv_contact.getSettings();
        settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        //                    settings.setBuiltInZoomControls(true); //显示放大缩小
        //                    settings.setSupportZoom(true); //可以缩放
        // webview自适应第二种
        settings.setUseWideViewPort(true);
        settings.setLoadWithOverviewMode(true);
        wv_contact.setSaveEnabled(true);
        settings.setJavaScriptEnabled(true); // 设置支持javascript脚本

        // 添加js交互接口类，并起别名 jsInterface
        wv_contact.addJavascriptInterface(new JSInterface(this), "jsInterface");

//        url = API.WEB_ADDRESS;
        switch (mType) {
            case 0://匹配
                url += API.MATCH_TABLE;
//                setScreenName("匹配申请表优惠活动");
                setScreenName("贷款-匹配申请表-优惠活动");
                break;
            case 1://产品
                url += API.PRODUCT_TABLE;
//                setScreenName("产品申请表优惠活动");
                setScreenName("贷款-产品申请表-优惠活动");
                break;
            case 2://已购买
                url += API.BOUGHT_TABLE;
//                setScreenName("贷款-已购买客户说明");
                setScreenName("贷款-已购买客户表-买断功能介绍");
                break;
            case 3://客户
                url += API.CUSTOMER_TABLE;
//                setScreenName("客户申请表优惠活动");
                setScreenName("信用卡-客户申请表-优惠活动");
                break;
            case 4://已购买
//                setScreenName("信用卡-已购买客户说明");
                setScreenName("信用卡-已购买客户表-买断功能介绍");
                break;
        }
        url += "?url=" + API.ACTIVITY_TABLE + "?SalesID=" + PreUtils.getUser(this).getSalesID();
        LogUtil.d(url);
        wv_contact.loadUrl(url);
    }

    @Override
    public void initData() {

    }

    @Override
    public void initAppComponent(AppComponent appComponent) {

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    /**
     * 活动关闭 回调
     *
     * @param updateDiscount
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(Event.UpdateDiscount updateDiscount) {
        finish();
    }

}
